#!/usr/bin/python
#coding = utf-8

import os
from pdfminer.pdfparser import PDFParser
from pdfminer.converter import PDFPageAggregator
from pdfminer.pdfparser import PDFParser
from pdfminer.pdfdocument import PDFDocument
from pdfminer.pdfpage import PDFPage
from pdfminer.pdfpage import PDFTextExtractionNotAllowed
from pdfminer.pdfinterp import PDFResourceManager
from pdfminer.pdfinterp import PDFPageInterpreter
from pdfminer.layout import *
import re

fp = open('RRT-2015-301216-1852-41.pdf', 'rb')

#Create a PDF parser object associated with the file object
parser = PDFParser(fp)
# Create a PDF document object that stores the document structure.
# Supply the password for initialization.
#document = PDFDocument(parser, password)
document = PDFDocument(parser)
# Check if the document allows text extraction. If not, abort.
if not document.is_extractable:
    raise PDFTextExtractionNotAllowed

# Create a PDF resource manager object that stores shared resources.
rsrcmgr = PDFResourceManager(caching = False)
# Set parameters for analysis.
laparams = LAParams()
# Create a PDF page aggregator object.
device = PDFPageAggregator(rsrcmgr, laparams=laparams)
# Create a PDF interpreter object.
interpreter = PDFPageInterpreter(rsrcmgr, device)


label = 0
patern = re.compile("Fisher")
replace=re.compile(r'\s+');
for page in PDFPage.create_pages(document):
    interpreter.process_page(page)
	# receive the LTPage object for the page.
    layout=device.get_result()
    for x in layout:
        if(isinstance(x,LTTextBoxHorizontal)):
            text=re.sub(replace,'',x.get_text())
            if len(text)!=0:
                #print text
				match = patern.match(text)
				if match:
					label = 1
					break
	if label:
		break

if label:
	#if exist the string remove it/rename it
	#os.remove('RRT-2015-301216-1852-41.pdf')
	os.rename('RRT-2015-301216-1852-41.pdf','1.pdf')
	print 0
else:
	print 1


				

